/*
*    微博评论模型
*/
import UIKit

class TMComments: NSObject {
    
    ///评论创建时间
    var created_at = String()
    ///评论的ID
    var id : Int = 0
    ///评论的内容
    var text = String()
    ///评论的来源
    var source = String()
    ///评论作者的用户信息字段
    var user: TMUser?
    ///评论的MID
    var mid = String()
    ///字符串型的评论ID
    var idstr = String()
    //是否为加载
    var load = false

  
    /// KVC字典转模型
    
  init(dict:[String : AnyObject]?){
        
        super.init()
    if dict == nil
    {
   return
    }
        setValuesForKeysWithDictionary(dict!)
    
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            
            let dict = value as! [String: AnyObject]
            let userModel = TMUser(dict: dict)
            self.user = userModel
            
            return
            
        }
        super.setValue(value, forKey: key)
        
    }
    
    
    class func changetomodel(arr : NSArray)->NSArray {
     let models = NSMutableArray()
        for dict in arr
        {
            let comment = TMComments(dict: dict as? [String : AnyObject])
            models.addObject(comment)
        }
        
        return models
    }
    // test
    
    
    //加载评论模型
    class func loadComment( since_id: Int, max_id: Int, id: Int?,finished: (coments: [TMComments]?, error: NSError?,total_number: Int,previous_cursor: Int,next_cursor: Int) -> ()){
        
        
        //调用网络方法请求评论
        
        CZNetworkTool.sharedInstance.commentsshow(since_id, max_id: max_id, id: id!) { (result, error) -> () in
            if result == nil && error != nil{
                
                print("获取评论数据失败")
            }
            
//            print("result\(result)")
            
            //总数量
            let total_number = result!["total_number"] as? Int
            
            // 上一页id.
            let previous_cursor = result?["previous_cursor"] as? Int
            
            // 下一页id
            let next_cursor = result?["next_cursor"] as? Int
            
            //字典转模型
            //空数组
            var comments = [TMComments]()
            
            //将转好的模型添加进数组.
            if let array = result?["comments"] as? [[String: AnyObject]]{
                for dict in array{
                    let comment = TMComments(dict: dict)
                    comments.append(comment)
                }
                
                //通过闭包返回 总数,
                finished(coments: comments, error: nil, total_number: total_number!, previous_cursor: previous_cursor!, next_cursor: next_cursor!)
                
            }
            
            
        }
        
        
    }
    
    

}
